package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teas = new Teacher[5];

    public boolean addTeacher(Teacher tea) {


        int index = -1;
        //2.2 遍历数组
        for (int i = 0; i < teas.length; i++) {
            //2.3 拿到每一个索引位置的元素
            Teacher teacher = teas[i];
            //2.4 判断当前元素是否为null
            if (teacher == null) {
                index = i;
                break;

            }
        }

        if (index == -1) {
            return false;
        } else {
            teas[index] = tea;
            return true;
        }

    }


    public Teacher[] findAllTeachers() {
        return teas;
    }
    //库管中的删除功能
    public void deleteTeacherById(String delId) {
        int index = getIdex(delId);

        teas[index] = null;
    }


    public int getIdex(String id) {

        int index = -1;
        //2.遍历数组容器
        for (int i = 0; i < teas.length; i++) {

            Teacher tea = teas[i];

            if (tea != null && tea.getId().equals(id)) {

                index = i;
                break;
            }
        }
        //3.返回index即可
        return index;
    }

    public void updateTeacherById(String updateId, Teacher newtea) {
        int index = getIdex(updateId);
        //2.将newStu 去覆盖掉该索引位置的旧对象,从而达到修改效果
        teas [index] = newtea;
    }
}

